import { Engine, Color3, FreeCamera, HemisphericLight, MeshBuilder, Scene, Vector3, StandardMaterial, ArcRotateCamera } from '@babylonjs/core'

// 构建场景的方法
const createScene = (canvas) => {
  // 创建渲染引擎
  const engine = new Engine(canvas)

  // 创建一个场景对象
  const scene = new Scene(engine)

  // 创建一个相机
  const camera = new ArcRotateCamera('camera1', -Math.PI / 2, Math.PI / 2, 2, new Vector3(0, 0, -0.5), scene)
  camera.setTarget(Vector3.Zero())

  // 拖动效果
  camera.attachControl(canvas, true)

  // 创建灯光
  const light = new HemisphericLight('light', new Vector3(0, 1, 0), scene)

  // 创建物体
  const box = MeshBuilder.CreateBox('box1', { size: 0.5 }, scene)

  // 定义盒子颜色
  const material = new StandardMaterial('box-material', scene)
  material.diffuseColor = Color3.Red()
  box.material = material

  // 根据浏览器渲染
  window.addEventListener('resize', () => {
    engine.resize()
  })
  // 渲染循环
  engine.runRenderLoop(() => {
    scene.render()
  })
}

export { createScene }
